﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using School.Entities;
using School.Common;

namespace School.Organization
{
    public static class Department
    {
        private static SchoolEntities db = new SchoolEntities();

        public static IQueryable<ORG_Department> All()
        {
            return from x in db.ORG_Department orderby x.Name select x;
        }

        public static IQueryable<ORG_Department> ByCampus(long campus)
        {
            return from x in db.ORG_Department where x.Campus_Id == campus orderby x.Name select x;
        }

        public static ORG_Department Single(int id)
        {
            var rows = from x in db.ORG_Department where x.Id == id select x;

            if (rows != null)
                return rows.First();
            else
                return null;
        }

        public static void Add(ORG_Department d)
        {
            db.AddToORG_Department(d);
            db.SaveChanges();
        }

        public static void Update(ORG_Department d)
        {
            var department = Single(d.Id);

            if (department != null)
            {
                department.Name = d.Name;
                department.Description = d.Description;
                department.Campus_Id = d.Campus_Id;
                db.SaveChanges();
            }
        }
    }
}
